//
//  HETWInputCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/18.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HETWInputCell.h"
#import "HESpeechListener.h"
#import "HESoundHUD.h"

@interface HETWInputCell ()<QMUITextViewDelegate>

@property(nonatomic, strong) QMUITextView *textView;

@property (nonatomic, strong) UIButton *leftLabel;
@property (nonatomic, strong) UIButton *yunyView;

@end

@implementation HETWInputCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    _leftLabel = [UIButton new];
    [self.contentView addSubview:_leftLabel];
    _leftLabel.userInteractionEnabled = NO;
    _leftLabel.titleLabel.font = DYBlodFont(18);
    _leftLabel.imageEdgeInsets = UIEdgeInsetsMake(0,-15, 0, 0);
    [_leftLabel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_leftLabel setImage:[UIImage imageNamed:@"3"] forState:UIControlStateNormal];
    [_leftLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    
    
    
    _textView = [QMUITextView  new];
    // 限制可输入的字符长度
    self.textView.placeholder = @"请填写备注信息，最多输入500个字!";
    self.textView.placeholderColor = CNavSubTitleColor; // 自定义 placeholder 的颜色
    self.textView.textContainerInset = UIEdgeInsetsMake(5, 7, 5, 7);
    self.textView.typingAttributes = @{NSFontAttributeName: DYFont(16),
                                       NSParagraphStyleAttributeName: [NSMutableParagraphStyle qmui_paragraphStyleWithLineHeight:20]
    };
    [self.contentView addSubview:_textView];
    [_textView setDelegate:self];
    
    
    _yunyView = [UIButton buttonWithType:UIButtonTypeCustom];
    [_yunyView setImage:IMAGE_NAMED(@"yuyin") forState:UIControlStateNormal];
//    _yunyView.contentMode = UIViewContentModeScaleAspectFill;
    [self.contentView addSubview: _yunyView];
    [_yunyView addTarget:self action:@selector(startSpeakClick) forControlEvents:UIControlEventTouchDown];
    [_yunyView addTarget:self action:@selector(stopSpeakClick) forControlEvents:UIControlEventTouchUpInside];
    [_yunyView addTarget:self action:@selector(stopSpeakClick) forControlEvents:UIControlEventTouchUpOutside];

    
    [_leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(20);
        make.left.equalTo(self.contentView).mas_offset(15);
    }];
    
    // 自适应title长度
    [_leftLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [_leftLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    
    [_yunyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_leftLabel);
        make.right.equalTo(self.contentView).mas_offset(-15);
        make.width.mas_offset(17);
        make.height.mas_offset(24);
    }];
 
    
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_leftLabel.mas_bottom).mas_offset(15);
        make.left.equalTo(self.contentView).mas_offset(15);
        make.right.equalTo(self.contentView).mas_offset(-15);
        make.bottom.equalTo(self.contentView).mas_offset(-15);
    }];
}

- (void)startSpeakClick {
 
    [[HESoundHUD sharedManager] show];
    
    HESpeechListener *sp = [HESpeechListener sharedManager];
    [sp startRecognizer];
    sp.textBlock = ^(NSString * _Nonnull text, BOOL status) {
        if (status) {
            self.textView.text = text;
        }else {
            [[HESoundHUD sharedManager] hide];
        }
    };
}

- (void)stopSpeakClick {
    [[HESpeechListener sharedManager] endRecognizer];
    [[HESoundHUD sharedManager] hide];
}

- (void)refreshContent:(GDFiterCellModel *)fiterModel formModel:(GDFormModel *)formModel indexPath:(NSIndexPath *)indexPath {
 
    [_leftLabel setTitle:fiterModel.leftTitle forState:UIControlStateNormal];
    [_leftLabel setImage:(fiterModel.isHasMustSigns?[UIImage imageNamed:@"3"]: nil) forState:UIControlStateNormal];
    
    _textView.placeholder = fiterModel.placeHolder;
    _textView.text = [formModel valueForKey:fiterModel.tableKey];
}

#pragma mark - <QMUITextViewDelegate>
// 实现这个 delegate 方法就可以实现自增高
- (void)textView:(QMUITextView *)textView newHeightAfterTextChanged:(CGFloat)height {
    
}

- (void)textView:(QMUITextView *)textView didPreventTextChangeInRange:(NSRange)range replacementText:(NSString *)replacementText {
    [Dialog toast:[NSString stringWithFormat:@"文字不能超过 %@ 个字符", @(textView.maximumTextLength)] delay:2.0];
}
 
- (void)textViewDidChange:(UITextView *)textView {
    [self.formModel setValue:textView.text forKey:self.fiterModel.tableKey]; // 将textField中的值赋给_formModel
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
